package cn.dansj.common.utils.transfer;

import java.security.SecureRandom;
import java.util.Random;
import java.util.UUID;
import java.util.stream.IntStream;

public abstract class RandomUtils {
    private static final char[] SPECIAL_CHARS = {'/', '+'};
    private static final SecureRandom RANDOM = new SecureRandom();

    private static final char[] LETTER_CHAR = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

    private static final char[] NUMERICAL_CHARS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

    private static final char[] ALPHANUMERICAL_CHARS = ArrayUtils.concat(LETTER_CHAR, NUMERICAL_CHARS);

    public static String getUid() {
        return UUID.randomUUID().toString().toLowerCase().replace("-", "");
    }

    public static int randomInt(int n1, int n2) {
        Random random = new Random();
        int b = Math.min(n1, n2);
        int e = Math.max(n1, n2);
        return random.nextInt(e - b + 1) + b;
    }

    public static int randomInt() {
        return randomInt(0, 10);
    }

    public static String random(final int count, final boolean letters, final boolean numbers) {
        AssertUtils.state(count >= 1, () -> new IllegalArgumentException("Count must be positive"));
        AssertUtils.state(letters || numbers, () -> new IllegalArgumentException("Either letters or numbers must be true"));
        final String chars = getCharsToUse(letters, numbers);
        return IntStream.range(0, count).map(i -> RANDOM.nextInt(chars.length())).map(chars::charAt).collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append).toString();
    }

    private static String getCharsToUse(boolean letters, boolean numbers) {
        if (letters && numbers) {
            return String.valueOf(ALPHANUMERICAL_CHARS);
        } else if (letters) {
            return String.valueOf(LETTER_CHAR);
        } else {
            return String.valueOf(NUMERICAL_CHARS);
        }
    }

    public static String random() {
        return random(25, true, true);
    }

    public static String sequence() {
        return System.currentTimeMillis() + random(2, false, true);
    }

    public static String generate(int length) {
        return generate(length, true);
    }

    public static String generate(int length, boolean special) {
        StringBuilder sb = new StringBuilder();
        char[] characters = special ? ArrayUtils.concat(SPECIAL_CHARS, ALPHANUMERICAL_CHARS) : ALPHANUMERICAL_CHARS;
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(characters.length);
            // 将随机选取的字符添加到字符串构建器中
            sb.append(characters[index]);
        }
        return sb.toString();
    }

    public static String randomCharacter(String characters) {
        return String.valueOf(characters.charAt(new Random().nextInt(characters.length())));
    }
}
